package com.yangrr.weblog.web.controller;

import com.yangrr.weblog.common.mail.MailHelperInteractExpired;
import com.yangrr.weblog.common.utils.Response;
import com.yangrr.weblog.web.model.vo.user.EmailDto;
import com.yangrr.weblog.web.model.vo.user.RegisterDto;
import com.yangrr.weblog.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;

/**
 * @PATH controller.com.yangrr.weblog.web.RegisterController
 * @Author YangRR
 * @CreateData 2025-05-08 13:45
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class RegisterController {

    @Autowired
    private UserService usersService;

    @Autowired
    private MailHelperInteractExpired mailHelperInteractExpired;

    @PostMapping("/register")
    public Response register(@RequestBody @Validated RegisterDto registerDto) {
        return usersService.register(registerDto);
    }

    @PostMapping("/sendCode")
    public Response sendMail(@RequestBody @Validated EmailDto emailDto) {

        // 发送邮件
        String code = Double.toString(Math.random()).substring(2, 8);

        LocalDateTime now = LocalDateTime.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(java.sql.Timestamp.valueOf(now));

        String html = String.format("<html>" +
                "<head>" +
                "<meta charset=\"UTF-8\">" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" +
                "<title>身份验证通知</title>" +
                "<style>" +
                "body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px; background-color: #f9f9f9; }" +
                ".container { background-color: #fff; border-radius: 8px; padding: 30px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05); }" +
                ".header { color: #2c3e50; border-bottom: 1px solid #eee; padding-bottom: 15px; margin-bottom: 20px; }" +
                ".verification-code { background-color: #f8f9fa; border-left: 4px solid #3498db; padding: 15px; margin: 20px 0; font-size: 24px; font-weight: bold; color: #e74c3c; text-align: center; letter-spacing: 3px; }" +
                ".footer { margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee; font-size: 14px; color: #7f8c8d; }" +
                ".note { background-color: #fff8e1; padding: 10px 15px; border-radius: 4px; margin: 15px 0; font-size: 14px; }" +
                ".signature { margin-top: 25px; font-style: italic; }" +
                "</style>" +
                "</head>" +
                "<body>" +
                "<div class=\"container\">" +
                "<div class=\"header\">" +
                "<h2>尊敬的用户：</h2>" +
                "</div>" +
                "<p>您好！</p>" +
                "<p>感谢您使用我们的服务。为确保您的账户安全，我们需要对您的身份进行验证。</p>" +
                "<div class=\"verification-code\">" +
                "您的验证码：%s" +
                "</div>" +
                "<div class=\"note\">" +
                "<strong>安全提示：</strong>此验证码将在 <span style=\"color: #e74c3c;\">1分钟</span> 内有效，请尽快使用。请勿向任何人透露此验证码，包括我们的客服人员。" +
                "</div>" +
                "<p>如非本人操作，请立即修改账户密码并联系我们的客服团队。</p>" +
                "<div class=\"footer\">" +
                "<p>此邮件为系统自动发送，请勿直接回复。</p>" +
                "<p>如有任何疑问，请联系我们的客服邮箱：3184130850@qq.com</p>" +
                "<p class=\"signature\">yangRR团队 敬上<br>%s</p>" +
                "</div>" +
                "</div>" +
                "</body>" +
                "</html>", code, formattedDate);
        boolean flag = mailHelperInteractExpired.sendHtml(emailDto.getEmail(), "注册验证码", html, code);
        return flag ? Response.success() : Response.fail();
    }
}
